<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Compose_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据商品ID得到组成信息
	 */
	public function find_by_product($productid){
		$this->db->select('t_compose.id,t_compose.materialid,t_nutrition.name,t_nutrition.image,t_nutrition.nengliang,t_nutrition.danbaizhi,t_nutrition.tanshui,t_nutrition.zhifang,t_compose.amount');
		$this->db->from('t_compose');
		$this->db->join('t_nutrition', 't_compose.materialid = t_nutrition.id','left');
		$this->db->where('productid',$productid);
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据商品ID得到配方信息
	 */
	public function count_by_product($productid){
		$this->db->select('sum(danbaizhi*amount)/100 as danbaizhi,sum(zhifang*amount)/100 as zhifang,sum(tanshui*amount)/100 as tanshui,sum(nengliang*amount)/100 as nengliang');
		$this->db->from('t_compose');
		$this->db->join('t_nutrition', 't_compose.materialid = t_nutrition.id','left');
		$this->db->where('productid',$productid);
		$query = $this->db->get();
		return $query->row_array();
	}
	/*
	 * 根据年龄和性别得到标准营养数值
	 */
	public function get_standard_nutrition($age,$gender){
		$this->db->select('danbaizhi,zhifang,tanshui,nengliang');
		$this->db->from('t_standard');
		$this->db->where('age',$age);
		$this->db->where('gender',$gender);
		$query = $this->db->get();
		return $query->row_array();
	}
}